var ctx=window.daijia.ctx;
var companyId=window.companyId;
var haszhuanche=window.haszhuanche;
var gradeId=window.gradeId;
var carTypeIdAreaIds = null;
$(document).ready(function(){
	
	queryCarTypes();
	
	getCarTypes();
	
	$(document).on("change",".carType_select",function() {
		getCarTypes();
	});
	
});

function getCarTypes(){
	var carType_divs=$(".tab-pane");
	var flag=false;
	carTypeIdAreaIds ="";
	for(var i=0;i<carType_divs.length;i++){
		var c=carType_divs[i];
		var carTypeId =$(c).attr("carTypeId");
		var area_divs = $(c).children("div");
		var carTypeArea="";
		$(area_divs).each(function(index,element){
			var areas = area_divs[index];
			var d = $(areas).children("div").children("select");
			var areaId = $(d).find("option:selected").val();
			if (areaId != null && areaId != "") {
				flag=true;
				var type = $(areas).attr("type");
				carTypeArea += ",'"+type+"':"+areaId;
			}
		});
		if (carTypeArea != "") {
			carTypeIdAreaIds +="{'carTypeId':"+carTypeId+carTypeArea;
		
			carTypeIdAreaIds += "},";
		}
		
	}
	if (flag) {
		carTypeIdAreaIds = "["+carTypeIdAreaIds
	
		carTypeIdAreaIds = carTypeIdAreaIds.substring(0,carTypeIdAreaIds.length-1);
	
		carTypeIdAreaIds += "]";
	}
}

$(document).on("change","#companyId",function(){
	companyId=$("#companyId").val();
	queryCarTypes();
});
$(document).on("click","li",function(){
	var a=$(this).children("a");
	var href=$(a).attr("href");
	
	var tab_pane=$(".tab-pane");
	for(var i=0;i<tab_pane.length;i++){
		var t=tab_pane[i];
		var id=$(t).attr("id");
		if(href!="#"+id){
			$(t).css("display","none");
		}else{
			$(t).css("display","block");
		}
	}
});
function queryCarTypes(){
	if(haszhuanche){
		$.ajax({
	        url : ctx+'/zhuanche/queryCarTypes',
	        type:'GET',
	        async:false,
	        dataType : 'json',
	        data : {
	        	"companyId":companyId,
	        },
	        success : function(result){
	        	var carTypeList="";
	        	$("#carTypeList").empty();
	        	$("#carType_div").empty();
	        	var carTypeSelect ="";
	        	
	        	if(result.success){
	        		$.ajax({
	        			url : ctx+'/member/queryGrade',
	        			type:'GET',
	        			async:false,
	        			dataType : 'json',
	        			data : {
	        				"id":gradeId,
	        			},
	        			success : function(data){
	        				if (data.success) {
	        					carTypeSelect = data.data;
	        				}
	        			}
	        		});
	        		$(result.carTypes).each(function(index,carType){
	        			var oldCarTypeArea = null;
	    				$(carTypeSelect).each(function(index,areas){
	    					if (carTypeSelect[index].carTypeId == carType.id) {
	    						oldCarTypeArea = carTypeSelect[index];
	    					}
						});
	    				
	        			var carType_div="";
	        			var carTypeName=carType.typeName;
	        			carTypeList+="<li><a href='#zhuanTab"+index+"' data-toggle='tab'>"+carTypeName+"</a></li>";
	        			if(index==0){
	        				carType_div="<div id='zhuanTab"+index+"' class='tab-pane active' carTypeId='"+carType.id+"'>";
	        			}else{
	        				carType_div="<div id='zhuanTab"+index+"' class='tab-pane' style='display:none' carTypeId='"+carType.id+"'>";
	        			}
	        			
	        			carType_div+="<div class='form-group' carTypeId='"+carType.id+"' type='tzAreaId'>"+
			                    "<label class='col-sm-3 control-label'>普通收费:</label>"+    
			                   	"<div class='col-sm-4'>"+
				                    "<select class='form-control carType_select'>"+
				                    	"<option value=''>--请选择--</option>";
				                    	for(var i=0;i<result.arealist.length;i++){
				                    		if (null != oldCarTypeArea && result.arealist[i].id == oldCarTypeArea.tzAreaId) {
				                    			carType_div+="<option value='"+result.arealist[i].id+"' selected='selected'>"+result.arealist[i].name+"</option>";
				                    		} else {
				                    			carType_div+="<option value='"+result.arealist[i].id+"'>"+result.arealist[i].name+"</option>";
				                    		}
				                    	}
	        	  carType_div+="</select>"+
			                	"</div>"+
			               "</div>"+
		                   "<div class='form-group' carTypeId='"+carType.id+"' type='rzAreaId'>"+
			                    "<label class='col-sm-3 control-label'>日租收费:</label>"+
			                    "<div class='col-sm-4'>"+
				                "<select class='form-control carType_select'>"+
				                	"<option value=''>--请选择--</option>";
				                	for(var i=0;i<result.arealist.length;i++){
				                		if (null != oldCarTypeArea && result.arealist[i].id == oldCarTypeArea.rzAreaId) {
			                    			carType_div+="<option value='"+result.arealist[i].id+"' selected='selected'>"+result.arealist[i].name+"</option>";
			                    		} else {
			                    			carType_div+="<option value='"+result.arealist[i].id+"'>"+result.arealist[i].name+"</option>";
			                    		}
			                    	}
	        	  carType_div+="</select>"+
			                        "</div>"+
			                        "</div>"+
			                        "<div class='form-group' carTypeId='"+carType.id+"' type='brzAreaId'>"+
			                        "<label class='col-sm-3 control-label'>半日租收费:</label>"+ 
			                        "<div class='col-sm-4'>"+
			                        "<select class='form-control carType_select'>"+
				               "<option value=''>--请选择--</option>";
				               for(var i=0;i<result.arealist.length;i++){
				            	   if (null != oldCarTypeArea && result.arealist[i].id == oldCarTypeArea.brzAreaId) {
				            		   carType_div+="<option value='"+result.arealist[i].id+"' selected='selected'>"+result.arealist[i].name+"</option>";
				            	   } else {
				            		   carType_div+="<option value='"+result.arealist[i].id+"'>"+result.arealist[i].name+"</option>";
				            	   }
				               }
	           	  carType_div+="</select>"+
			                	"</div>"+
			               "</div>"+
			                "<div class='form-group' carTypeId='"+carType.id+"' type='jjAreaId'>"+
			                 "<label class='col-sm-3 control-label'>接机收费:</label>   "+ 
			                  "<div class='col-sm-4'>"+
				               "<select class='form-control carType_select'>"+
				                "<option value=''>--请选择--</option>";
				                for(var i=0;i<result.arealist.length;i++){
				                	if (null != oldCarTypeArea && result.arealist[i].id == oldCarTypeArea.jjAreaId) {
				                		carType_div+="<option value='"+result.arealist[i].id+"' selected='selected'>"+result.arealist[i].name+"</option>";
				                	} else {
				                		carType_div+="<option value='"+result.arealist[i].id+"'>"+result.arealist[i].name+"</option>";
				                	}
		                    	}
	        	  carType_div+="</select>"+
			                	"</div>"+
			                "</div>"+
			               "<div class='form-group' carTypeId='"+carType.id+"' type='sjAreaId'>"+
			                   "<label class='col-sm-3 control-label'>送机收费:</label>    "+
			                   	"<div class='col-sm-4'>"+
				                "<select class='form-control carType_select'>"+
				               "<option value=''>--请选择--</option>";
				               for(var i=0;i<result.arealist.length;i++){
				            	   if (null != oldCarTypeArea && result.arealist[i].id == oldCarTypeArea.sjAreaId) {
				            		   carType_div+="<option value='"+result.arealist[i].id+"' selected='selected'>"+result.arealist[i].name+"</option>";
				            	   } else {
				            		   carType_div+="<option value='"+result.arealist[i].id+"'>"+result.arealist[i].name+"</option>";
				            	   }
		                    	}
	           	   carType_div+="</select>"+
			                "</div>"+
			               "</div>"+
			               "<div class='form-group' carTypeId='"+carType.id+"' type='jzAreaId'>"+
			                  "<label class='col-sm-3 control-label'>接站收费:</label> "+   
			                   "<div class='col-sm-4'>"+
				                "<select class='form-control carType_select'>"+
				                "<option value=''>--请选择--</option>";
				                for(var i=0;i<result.arealist.length;i++){
				                	if (null != oldCarTypeArea && result.arealist[i].id == oldCarTypeArea.jzAreaId) {
				                		carType_div+="<option value='"+result.arealist[i].id+"' selected='selected'>"+result.arealist[i].name+"</option>";
				                	} else {
				                		carType_div+="<option value='"+result.arealist[i].id+"'>"+result.arealist[i].name+"</option>";
				                	}
				                }
	        	   carType_div+="</select>"+
			               "</div>"+
			               "</div>"+
			               "<div class='form-group' carTypeId='"+carType.id+"' type='szAreaId'>"+
			                  "<label class='col-sm-3 control-label'>送站收费:</label>    "+
			                  "<div class='col-sm-4'>"+
				                  "<select class='form-control carType_select'>"+
				                    	"<option value=''>--请选择--</option>";
				                    	for(var i=0;i<result.arealist.length;i++){
				                    		if (null != oldCarTypeArea && result.arealist[i].id == oldCarTypeArea.szAreaId) {
						                		carType_div+="<option value='"+result.arealist[i].id+"' selected='selected'>"+result.arealist[i].name+"</option>";
						                	} else {
						                		carType_div+="<option value='"+result.arealist[i].id+"'>"+result.arealist[i].name+"</option>";
						                	}
				                    	}
	               carType_div+="</select>"+
			                	"</div>"+
			                "</div>"+
		                "</div>";
	        			
		                $("#carType_div").append(carType_div);
	        		});
	        		
	        		$("#carTypeList").append(carTypeList);
	        		
	        		
	        	}else{
	        		$("#carTypeList").append("<span style='color:red'>"+result.message+"</span>");
	        	}
	        }
	        
		});
	}
}

$("#submit_button").click(function(){
	 if(parseInt($("#parallelOrderNumber").val()) < 1){
         $.scojs_message("并行订单量不能小于1!", $.scojs_message.TYPE_ERROR);
         return false;
     }

     if(parseInt($("#limitAmounts").val())>parseInt($("#parallelOrderNumber").val())){
         $.scojs_message("一次性下单量不能超过并行下单量!", $.scojs_message.TYPE_ERROR);
         return false;
     }
     
	$("#submit_button").attr("disabled",true);
	if (null != carTypeIdAreaIds && carTypeIdAreaIds != "") {
		$.ajax({
			type: "POST",
			url: ctx+"/member/updateGrade",
			data: {
				id:$("#id").val(),
				name:$("#name").val(),
				companyId:$("#companyId").val(),
				parallelOrderNumber:$("#parallelOrderNumber").val(),
				companyName:$('#companyId').find("option:selected").text(),
				defaultGrade:$("input[type='radio']:checked").val(),
				limitAmounts:$("#limitAmounts").val(),
				daijiaAreaId:$("#daijiaAreaId").val(),
				carTypes:carTypeIdAreaIds
			},	
			dataType: "json",
			success: function(data){ 
				if(data.success){
					$.scojs_message('修改成功', $.scojs_message.TYPE_OK);
					setTimeout(function(){
						window.location=ctx+'/member/gradelist';
					},1000);
				}else{
					$("#submit_button").removeAttr("disabled");
					$.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
				}
			}
		});
	} else {
		$.ajax({
			type: "POST",
			url: ctx+"/member/updateGrade",
			data: {
				id:$("#id").val(),
				name:$("#name").val(),
				companyId:$("#companyId").val(),
				parallelOrderNumber:$("#parallelOrderNumber").val(),
				companyName:$('#companyId').find("option:selected").text(),
				defaultGrade:$("input[type='radio']:checked").val(),
				limitAmounts:$("#limitAmounts").val(),
				daijiaAreaId:$("#daijiaAreaId").val()
			},	
			dataType: "json",
			success: function(data){ 
				if(data.success){
					$.scojs_message('修改成功', $.scojs_message.TYPE_OK);
					setTimeout(function(){
						window.location=ctx+'/member/gradelist';
					},1000);
				}else{
					$("#submit_button").removeAttr("disabled");
					$.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
				}
			}
		});
	}
});
